package com.springboot.demo.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.springboot.demo.entity.ChbUser;

public class ExcelUpload {
	
	private static Logger log = LoggerFactory.getLogger(ExcelUpload.class);

	@SuppressWarnings("all")
	public static List<ChbUser> readExcelValue(File tempFile) throws IOException{
		// 实例化输入流
		InputStream is = new FileInputStream(tempFile);
		
		// 创建weekbook
		XSSFWorkbook wb = new XSSFWorkbook(is);
		
		// 错误信息接收器
		String errorMsg = "";
		
		 // 得到第一个shell  
	    Sheet sheet=wb.getSheetAt(0);
	    
	    // 得到Excel的行数
	    int totalRows = sheet.getPhysicalNumberOfRows();
	    
	    // 总列数
	    int totalCells = 0;
	    
	    // 得到Excel的列数(前提是有行数)，从第一行开始
	    if(totalRows >= 0 && sheet.getRow(0) != null){
	    	totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
	    }else{
	    	errorMsg = "excel里没有数据";
	    }
	    
	    List<ChbUser> userList = new ArrayList<ChbUser>();
	    ChbUser chbUser;
	    
	    String br = "<br/>";
	    
	    //循环Excel行数,从第一行开始
	    for(int r=0;r<totalRows;r++){
	    	String rowMessage = "";
	        Row row = sheet.getRow(r);
	        if (row == null){
	        	errorMsg += br+"第"+(r+1)+"行数据有问题，请仔细检查！";
	        	log.error("---->"+br+"第"+(r+1)+"行数据有问题，请仔细检查！");
	        	continue;
	        }
	        chbUser = new ChbUser();
	        
	        String nickName = "";
	        String phoneNumber = "";
	        String headImageUrl = Constants.HEAD_IMAGE;
	        //循环Excel的列
	        for(int c = 0; c <totalCells; c++){
	            Cell cell = row.getCell(c);
	            if (null != cell){
	                if(c==0){
	                	cell.setCellType(Cell.CELL_TYPE_STRING);
	                	nickName = cell.getStringCellValue();
	                	if(StringUtils.isEmpty(nickName)){
	                		rowMessage += "昵称不能为空；";
	                	}else if(nickName.length()>60){
	                		rowMessage += "昵称的字数不能超过60；";
	                	}
	                	chbUser.setNickName(nickName);
	                }else if(c==1){
	                	cell.setCellType(Cell.CELL_TYPE_STRING);
	                	phoneNumber = cell.getStringCellValue();
	                	if(StringUtils.isEmpty(phoneNumber)){
	                		rowMessage += "电话号码不能为空；";
	                	}else if(phoneNumber.length()>11){
	                		rowMessage += "电话号码的字数不能超过11位；";
	                	}
	                	chbUser.setPhoneNumber(phoneNumber);
	                }
	                // 存入默认头像
	                chbUser.setHeadImageUrl(headImageUrl);
	            }else{
	             rowMessage += "第"+(c+1)+"列数据有问题，请仔细检查；";
	             log.error("---->"+br+"第"+(c+1)+"列数据有问题，请仔细检查!");
	            }
	        }
	        //拼接每行的错误提示
	        if(!StringUtils.isEmpty(rowMessage)){
	        	errorMsg += br+"第"+(r+1)+"行，"+rowMessage;
	        	log.error("---->"+br+"第"+(r+1)+"行，"+rowMessage);
	        }else{
	        	userList.add(chbUser);
	        }
	    }
	    
		return userList;
	}
	
}
